<?php
	include_once("$config[webroot]/includes/page_utf_class.php");

	if($_POST['act']=='op')
	{
		if(!empty($_POST['chk']))
		{
			@$id=implode(",",$_POST['chk']);
			//批量删除
			if($id)
			{ 
				$sql="delete from ".ACTIVITYPRODUCT." where product_id in ($id)";
				$db->query($sql);
			}
			//审核通过
			if($_POST['submit']==$lang['tg'] and $id)
			{
				$sql="update ".ACTIVITYPRODUCT." set status='2' where product_id in ($id)";	
				$re=$db->query($sql);
			}
			//审核不通过
			if($_POST['submit']==$lang['btg'] and $id)
			{
				$sql="update ".ACTIVITYPRODUCT." set status='3' where product_id in ($id)";	
				$re=$db->query($sql);
			}
		}
		if($_POST['displayorder'])
		{
			foreach($_POST['displayorder'] as $key=>$list)
			{
				$db->query("update ".ACTIVITYPRODUCT." set displayorder='$list' where id='$key'");
			}
		}
		unset($_GET['s']);
		unset($_GET['m']);
		$getstr=implode('&',convert($_GET));
		msg("?m=activity&s=activity_product_list.php&$getstr");
	}
	
	if(!empty($_GET['t']) and is_numeric($_GET['t']))
	{
		$str=" and b.status = '$_GET[t]' ";
	}
	if(!empty($_GET['editid']) and is_numeric($_GET['editid']))
	{
		$str=" and b.activity_id = '$_GET[editid]' ";
	}
	
	//获取产品列表
	$sql="SELECT a.id,a.catid,a.brand,a.tags,a.price,a.show_price,a.market_price,a.pic,a.code,a.pname,a.stock_shop,b.*  FROM ".ACTIVITYPRODUCT." b left join ".PRO." a on a.id=b.product_id WHERE 1 $str order by b.displayorder,create_time desc";
	$export_sql = "SELECT a.id,a.create_time,b.displayorder FROM ".ACTIVITYPRODUCT." b left join ".PRO." a on a.id=b.product_id WHERE 1 $str order by b.displayorder,create_time desc";
	//分页
	include_once("../includes/page_utf_class.php");
	$page = new Page;
	$page->listRows=20;
	if (!$page->__get('totalRows')){
		$db->query($sql);
		$page->totalRows = $db->num_rows();
	}
	$sql .= "  limit ".$page->firstRow.",20";
	$de['page'] = $page->prompt();
	
	$db->query($sql);
	$de['list']=$db->getRows();

	foreach($de['list'] as $key=>$val)
	{
		
	    //获取产品类别
	    $tmp=array();
	    if ($val['catid']){
	        $sql = "select cat from ".PCAT." where '$val[catid]' like concat(`catid`,'%')  order by catid asc";
	        $db->query($sql);
	        foreach ($cat=$db->getRows() as $item){
	            $tmp[]=$item['cat'];
	        }
	    }
	     
	    $tmp2=explode(',', $val['pic']);
	    $de['list'][$key]['pic']=$tmp2[0];
	    
	    $de['list'][$key]['cat']=implode('->', $tmp);
		
	}
	
	//产品标签
	$tagids = "";
	$tagTmp = array();
	foreach ($de['list'] as $dk=>$dv)
	{
		if($dv['tags']){
	    $tagids .= $dv['tags'].",";
		}
	}
	$tagids = rtrim($tagids,",");
	if($tagids!=""){
	    $sql="SELECT id,tags FROM ".TAG." WHERE id IN (".$tagids.")";
	    $db->query($sql);
	    $tagTmp=$db->getRows();
	}
	
	$tagArr = array();
	foreach ($tagTmp as $tav)
	{
	    $tagArr[$tav['id']] = $tav['tags'];
	}
	
	
	foreach ($de['list'] as $dk=>$dv)
	{
	    $tagsTmp = explode(",", $dv['tags']);
	    $tagStr = "";
	    foreach ($tagsTmp as $ttv)
	    {
	        $tagStr .= $tagArr[$ttv].",";
	    }
	    $de['list'][$dk]['tagnames'] = rtrim($tagStr,",");
	}
	
	if(isset($_GET['export']) && $_GET['export']==1){
		include_once '../lib/php_excel.php';
		$db->query($export_sql);
		$export_list=$db->getRows();
		foreach ($export_list as $k=>$val)
		{
			unset($export_list[$k]['create_time']);
			unset($export_list[$k]['displayorder']);
		}
		$arr[0] = array("ID");
		$export_list = array_merge($arr,$export_list);
		$filename =   "activityProductID" . date ( "YmdHis" );
		
		$xls = new Excel_XML ( 'UTF-8', false, 'activityProductID' );
	    $xls->addArray ( $export_list );
	    $xls->generateXML ( $filename );
		exit;
	}
	
	$condTemp = $_GET;
	unset($condTemp['m']);
	unset($condTemp['s']);
	$cond = "";
	if(count($condTemp)){
		$cond = "&".http_build_query($condTemp);
	}
	
	$tpl->assign("act_list_url",'module.php?m=activity&s=activity.php&'.$cond);
	$tpl->assign("de",$de);
	$tpl->assign("config",$config);
	$tpl->display("activity_product_list.htm");

?>